//
//  WKWebViewController.m
//  KaneNote
//
//  Created by HuanJing on 2017/7/14.
//  Copyright © 2017年 HuanJing. All rights reserved.
//

#import "WKWebViewController.h"

@interface WKWebViewController ()

@end

@implementation WKWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    UITextView *textView = [[UITextView alloc] initWithFrame:kSCREEN];
    textView.editable = NO;
    textView.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:textView];
    
    NSString *htmlString = [NSString stringWithFormat:@"%@/%@", HOST, self.srcString];
    NSData *htmlData = [NSData dataWithContentsOfURL:[NSURL URLWithString:htmlString]];
    TFHpple *xPathParser = [[TFHpple alloc] initWithHTMLData:htmlData encoding:HTML_ENCODING];
    NSArray *elements = [xPathParser searchWithXPathQuery:@"//td[@class='diablo_content_title']//p"];
    NSMutableArray *textArray = [NSMutableArray array];
    for (TFHppleElement *ele in elements) {
        NSLog(@"%@", [ele content]);
        NSString *str_1 = [self sortOutString:[ele content]];
        [textArray addObject:str_1];
    }
    NSString *fullStr = [textArray componentsJoinedByString:@"\n        "];
    textView.text = fullStr;
    
    
//    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/%@", HOST, self.srcString]];
//    WKWebView *webView = [[WKWebView alloc] initWithFrame:kSCREEN];
//    [webView loadRequest:[NSURLRequest requestWithURL:url]];
//    [self.view addSubview:webView];
    
    
}

- (NSString *)sortOutString:(NSString *)str {
    NSMutableString *sortedStr = [NSMutableString stringWithString:str];
    //去掉首位的空白字符和换行字符
    NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
    NSArray *arr1 = [sortedStr componentsSeparatedByCharactersInSet:set];
    NSString *noSpaceStr = [arr1 componentsJoinedByString:@""];
    return noSpaceStr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
